package enums;

import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;

public class PayEnum {


	/**
	 * @ClassName OrderStatusEnum
	 * @Deacription 订单状态
	 **/
	public static enum OrderStatusEnum {
		SUCCESS(1, "SUCCESS","成功"),
		FAILURE(2, "FAILURE","失败"),
		CLOSE(3, "CLOSE","超时关闭或全额退款"),
		NEED_CONFIRM(4, "NEED_CONFIRM","待确认"),
		COMPLETE(5, "COMPLETE","完成"),
		UN_KNOW(6, "UN", "未定义的交易订单状态"),
		;
		private Integer id;

		private String code;

		private String desc;

		OrderStatusEnum(Integer id, String code, String desc){
			this.id = id;
			this.code = code;
			this.desc= desc;
		}

		public Integer getId(){
			return id;
		}

		public String getCode(){
			return code;
		}

		public String getDesc(){
			return desc;
		}

		public static OrderStatusEnum getOrderStatusEnum(Integer id) {
			if(null==id){
				return OrderStatusEnum.UN_KNOW;
			}
			return Arrays.stream(OrderStatusEnum.values())
					.filter(p -> p.getId().equals(id))
					.findAny()
					.orElse(OrderStatusEnum.UN_KNOW);
		}

		public static OrderStatusEnum getOrderStatusEnum(String code) {
			if(StringUtils.isBlank(code)){
				return OrderStatusEnum.UN_KNOW;
			}
			return Arrays.stream(OrderStatusEnum.values())
					.filter(p -> p.getCode().equals(code))
					.findAny()
					.orElse(OrderStatusEnum.UN_KNOW);
		}
	}


	/**
	 * @ClassName 支付交易状态
	 * @Deacription
	 **/
	public static enum PayTradeStatusEnum {
		/**支付宝支付交易状态**/
		WAIT_BUYER_PAY(OrderStatusEnum.NEED_CONFIRM.code, PayChannelEnum.ALI_PAY.getId(),"WAIT_BUYER_PAY", "交易创建，等待买家付款"),
		TRADE_CLOSED(OrderStatusEnum.CLOSE.code, PayChannelEnum.ALI_PAY.getId(),"TRADE_CLOSED","未付款交易超时关闭，或支付完成后全额退款"),
		TRADE_SUCCESS(OrderStatusEnum.SUCCESS.code, PayChannelEnum.ALI_PAY.getId(),"TRADE_SUCCESS","交易成功"),
		TRADE_FAILURE(OrderStatusEnum.FAILURE.code, PayChannelEnum.ALI_PAY.getId(),"TRADE_FAILURE","交易失败"),
		TRADE_FINISHED(OrderStatusEnum.SUCCESS.code, PayChannelEnum.ALI_PAY.getId(),"TRADE_FINISHED","交易结束，不可退款"),

		/**微信支付交易状态**/
		NOTPAY(OrderStatusEnum.NEED_CONFIRM.code, PayChannelEnum.WX_PAY.getId(),"NOTPAY", "未支付"),
		REFUND(OrderStatusEnum.CLOSE.code, PayChannelEnum.WX_PAY.getId(),"REFUND","订单已转入退款"),
		SUCCESS(OrderStatusEnum.SUCCESS.code, PayChannelEnum.WX_PAY.getId(),"SUCCESS","支付成功"),
		CLOSED(OrderStatusEnum.CLOSE.code, PayChannelEnum.WX_PAY.getId(),"CLOSED","订单已关闭"),
		REVOKED(OrderStatusEnum.CLOSE.code, PayChannelEnum.WX_PAY.getId(),"REVOKED","订单已撤销"),
		USERPAYING(OrderStatusEnum.NEED_CONFIRM.code, PayChannelEnum.WX_PAY.getId(),"USERPAYING","用户支付中"),
		PAYERROR(OrderStatusEnum.FAILURE.code, PayChannelEnum.WX_PAY.getId(),"PAYERROR","支付失败(其他原因，如银行返回失败)"),

		/**其他未知状态**/
		UN_KNOW(OrderStatusEnum.UN_KNOW.code, PayChannelEnum.UN_KNOW.getId(), "UN", "未知交易状态"),

		;

		PayTradeStatusEnum(String code, Integer payChannel, String thirdCode, String desc){
			this.code = code;
			this.thirdCode = thirdCode;
			this.payChannel = payChannel;
			this.desc= desc;
		}

		private String code;

		private String thirdCode;

		private Integer payChannel;

		private String desc;

		public String getCode(){
			return code;
		}

		public String getThirdCode(){
			return thirdCode;
		}

		public Integer getPayChannel(){
			return payChannel;
		}

		public String getDesc(){
			return desc;
		}

		public static PayTradeStatusEnum getByCode(String code, Integer payChannel){
			if(StringUtils.isBlank(code)
					||null==payChannel){
				return PayTradeStatusEnum.UN_KNOW;
			}
			return Arrays.stream(PayTradeStatusEnum.values())
					.filter(p -> p.payChannel.equals(payChannel))
					.filter(p -> p.code.equals(code))
					.findAny()
					.orElse(PayTradeStatusEnum.UN_KNOW);
		}

		public static PayTradeStatusEnum getByThirdCode(String thirdCode, Integer payChannel){
			if(StringUtils.isBlank(thirdCode)
				||null==payChannel){
				return PayTradeStatusEnum.UN_KNOW;
			}
			return Arrays.stream(PayTradeStatusEnum.values())
					.filter(p -> p.payChannel.equals(payChannel))
					.filter(p -> p.thirdCode.equals(thirdCode))
					.findAny()
					.orElse(PayTradeStatusEnum.UN_KNOW);
		}
	}


	/**
	 * @ClassName 退款交易状态
	 * @Deacription
	 **/
	public enum RefundTradeStatusEnum {

		ALI_SUCCESS(OrderStatusEnum.SUCCESS.code, PayChannelEnum.ALI_PAY.getId(), "Success","退款成功"),

		WX_SUCCESS(OrderStatusEnum.SUCCESS.code, PayChannelEnum.WX_PAY.getId(), "SUCCESS","退款成功"),
		WX_CHANGE(OrderStatusEnum.FAILURE.code, PayChannelEnum.WX_PAY.getId(), "CHANGE","退款异常"),
		WX_REFUND_CLOSE(OrderStatusEnum.CLOSE.code, PayChannelEnum.WX_PAY.getId(), "REFUNDCLOSE","退款关闭"),

		UN_KNOW(OrderStatusEnum.UN_KNOW.code, PayChannelEnum.WX_PAY.getId(), "UN", "未知交易状态"),

		;

		RefundTradeStatusEnum(String code, Integer payChannel, String thirdCode, String desc){
			this.code = code;
			this.thirdCode = thirdCode;
			this.payChannel = payChannel;
			this.desc= desc;
		}

		private String code;

		private String thirdCode;

		private Integer payChannel;

		private String desc;


		public String getCode(){
			return code;
		}

		public String getThirdCode(){
			return thirdCode;
		}

		public Integer getPayChannel(){
			return payChannel;
		}

		public String getDesc(){
			return desc;
		}

		public static RefundTradeStatusEnum getByCode(String code, Integer payChannel){
			if(StringUtils.isBlank(code)
				||payChannel==null){
				return RefundTradeStatusEnum.UN_KNOW;
			}
			return Arrays.stream(RefundTradeStatusEnum.values())
					.filter(p -> p.payChannel.equals(payChannel))
					.filter(p -> p.code.equals(code))
					.findAny()
					.orElse(RefundTradeStatusEnum.UN_KNOW);
		}

		public static RefundTradeStatusEnum getByThirdCode(String thirdCode, Integer payChannel){
			if(StringUtils.isBlank(thirdCode)
				||payChannel==null){
				return RefundTradeStatusEnum.UN_KNOW;
			}
			return Arrays.stream(RefundTradeStatusEnum.values())
					.filter(p -> p.payChannel.equals(payChannel))
					.filter(p -> p.thirdCode.equals(thirdCode))
					.findAny()
					.orElse(RefundTradeStatusEnum.UN_KNOW);
		}
	}
}